package com.skyolder.milkbee.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.skyolder.milkbee.domain.Requisition;
import com.skyolder.milkbee.service.RequisitionService;
import com.skyolder.milkbee.web.rest.util.HeaderUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing Requisition.
 */
@RestController
@RequestMapping("/api")
public class RequisitionResource {

    private final Logger log = LoggerFactory.getLogger(RequisitionResource.class);
        
    @Inject
    private RequisitionService requisitionService;

    /**
     * POST  /requisitions : Create a new requisition.
     *
     * @param requisition the requisition to create
     * @return the ResponseEntity with status 201 (Created) and with body the new requisition, or with status 400 (Bad Request) if the requisition has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/requisitions")
    @Timed
    public ResponseEntity<Requisition> createRequisition(@RequestBody Requisition requisition) throws URISyntaxException {
        log.debug("REST request to save Requisition : {}", requisition);
        if (requisition.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("requisition", "idexists", "A new requisition cannot already have an ID")).body(null);
        }
        Requisition result = requisitionService.save(requisition);
        return ResponseEntity.created(new URI("/api/requisitions/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("requisition", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /requisitions : Updates an existing requisition.
     *
     * @param requisition the requisition to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated requisition,
     * or with status 400 (Bad Request) if the requisition is not valid,
     * or with status 500 (Internal Server Error) if the requisition couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/requisitions")
    @Timed
    public ResponseEntity<Requisition> updateRequisition(@RequestBody Requisition requisition) throws URISyntaxException {
        log.debug("REST request to update Requisition : {}", requisition);
        if (requisition.getId() == null) {
            return createRequisition(requisition);
        }
        Requisition result = requisitionService.save(requisition);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("requisition", requisition.getId().toString()))
            .body(result);
    }

    /**
     * GET  /requisitions : get all the requisitions.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of requisitions in body
     */
    @GetMapping("/requisitions")
    @Timed
    public List<Requisition> getAllRequisitions() {
        log.debug("REST request to get all Requisitions");
        return requisitionService.findAll();
    }

    /**
     * GET  /requisitions/:id : get the "id" requisition.
     *
     * @param id the id of the requisition to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the requisition, or with status 404 (Not Found)
     */
    @GetMapping("/requisitions/{id}")
    @Timed
    public ResponseEntity<Requisition> getRequisition(@PathVariable Long id) {
        log.debug("REST request to get Requisition : {}", id);
        Requisition requisition = requisitionService.findOne(id);
        return Optional.ofNullable(requisition)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /requisitions/:id : delete the "id" requisition.
     *
     * @param id the id of the requisition to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/requisitions/{id}")
    @Timed
    public ResponseEntity<Void> deleteRequisition(@PathVariable Long id) {
        log.debug("REST request to delete Requisition : {}", id);
        requisitionService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("requisition", id.toString())).build();
    }

}
